<?php

class YimeEmail
{
    /**
     * @param string $sendTo
     * @param string $subject
     * @param string $content
     * @return bool
     */
    public static function sendMail($sendTo, $subject, $content)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->load->library('email');

        $email = new CI_Email(array(
            'protocol'     => 'smtp',
            'smtp_host'    => 'ssl://smtp.exmail.qq.com',
            'smtp_port'    => '465',
            'smtp_timeout' => '30',
            'smtp_user'    => 'postmaster@hiyime.com',
            'smtp_pass'    => '80tFMKJT',
            'charset'      => 'utf-8',
            'mailtype'     => 'html',
            'wordwrap'     => TRUE,
            'crlf'         => "\r\n", //QQ邮箱必须用
            'newline'      => "\r\n",//QQ邮箱必须用

        ));

        if (!isset($sendTo) || !$sendTo)
            return false;

        $email->from($email->smtp_user, "Yime运营团队");
        $email->to($sendTo);
        $email->subject($subject);
        $email->message($content);

        $result = $email->send();
        if($result){
            return true;
        }else{
            return false;
        }
    }

    public static function sendMailRegisterSuccess($sendTo, $activationCode)
    {
        //Todo 文案_(:3」∠)_

        $subject = '欢迎加入到 Yime';
        $content=<<<EOT
        <html>
        <head><base target="_blank">
        </head>
        <body>
        <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
        <tr>
        <td bgcolor="#f7f9fa" align="center" style="padding:22px 0 20px 0" class="responsive-table">
        <table border="0" cellpadding="0" cellspacing="0" style="background-color:f7f9fa; border-radius:3px;border:1px solid #dedede;margin:0 auto; background-color:#ffffff" width="552" class="responsive-table">
        <tbody>
        <tr>
        <td bgcolor="#59bb75" height="54" align="center" style="border-top-left-radius:3px;border-top-right-radius:3px;">
        <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
        <tr>
        <td align="center" class="zhwd-high-res-img-wrap zhwd-zhihu-logo"><a href="http://www.hiyime.com" style="text-decoration:none;color:#fff;font-weight:bold;font-family:微软雅黑;font-size:20px;">Yime易米网</a>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        <tr>
        <td bgcolor="#ffffff" align="center" style="padding: 0 15px 0px 15px;">
        <table border="0" cellpadding="0" cellspacing="0" width="480" class="responsive-table">
        <tbody><tr>
        <td>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tbody>
        <tr>
        <td>
        <table cellpadding="0" cellspacing="0" border="0" align="left" class="responsive-table">
        <tbody><tr>
        <td width="550" align="left" valign="top">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tbody><tr>
        <td bgcolor="#ffffff" align="left" style="background-color:#ffffff; font-size: 17px; color:#7b7b7b; padding:28px 0 0 0;line-height:25px;"><b>##EMAIL##，你好！</b>
        </td>
        </tr>
        <tr>
        <td align="left" valign="top" style="font-size:15px; color:#7b7b7b; font-size:14px; line-height: 25px; font-family:Hiragino Sans GB; padding: 15px 0px 25px 0px">Yime易米网已经收到了你的注册请求，请 24 小时内点击下面的按钮激活帐号。
        </td>
        </tr>
        <tr>
        <td style="border-top:1px #f1f4f6 solid; padding: 26px 0 32px 0;" align="center" class="padding">
        <table border="0" cellspacing="0" cellpadding="0" class="responsive-table">
        <tbody><tr>
        <td>
        <span style="font-family:Hiragino Sans GB;;font-size:17px;color:#0a82e4">
        <a style="text-decoration:none;color:#ffffff;" href="##LINK##" target="_blank">
        <div style="padding:10px 25px 10px 25px;border-radius:3px;text-align:center;text-decoration:none;background-color:#59bb75;color:#ffffff;font-size:17px;margin:0;white-space:nowrap">激活帐号
        </div>
        </a>
        </span>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>

        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tbody><tr>
        <td bgcolor="#f7f9fa" align="center">
        <table width="552" border="0" cellpadding="0" cellspacing="0" align="center" class="responsive-table">
        <tbody><tr>
        <td align="center" valign="top" bgcolor="#f7f9fa" style="font-family:Hiragino Sans GB; font-size:12px; color:#b6c2cc; line-height:17px; padding:0 0 25px 0;">这封邮件的收件地址是 <a href="mailto:##EMAIL##">##EMAIL##</a> <br>
        你可以通过<a href="" style="border:none;color:#8a939b;text-decoration:none;">&nbsp;设置&nbsp;</a><span>管理其他来自Yime易米网的邮件</span> <br>
        © 2014 Yime易米网
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        <style type="text/css">
        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}
        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word}
        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
        img{ border:0}
        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
        </style>

        <style id="ntes_link_color" type="text/css">a,td a{color:#064977}</style></body></html>
EOT;
        $content=str_replace('##LINK##',base_url('home/activation/'.$activationCode),$content);
        $content=str_replace('##EMAIL##',$sendTo,$content);
        return self::sendMail($sendTo, $subject, $content);
    }

    public static function sendEmailResetPwd($sendTo, $resetPwd_Token)
    {
        //Todo 文案_(:3」∠)_

        $subject = '欢迎加入到 Yime';
        $content=<<<EOT
        <html>
        <head><base target="_blank">
        </head>
        <body>
        <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
        <tr>
        <td bgcolor="#f7f9fa" align="center" style="padding:22px 0 20px 0" class="responsive-table">
        <table border="0" cellpadding="0" cellspacing="0" style="background-color:f7f9fa; border-radius:3px;border:1px solid #dedede;margin:0 auto; background-color:#ffffff" width="552" class="responsive-table">
        <tbody>
        <tr>
        <td bgcolor="#59bb75" height="54" align="center" style="border-top-left-radius:3px;border-top-right-radius:3px;">
        <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
        <tr>
        <td align="center" class="zhwd-high-res-img-wrap zhwd-zhihu-logo"><a href="http://www.hiyime.com" style="text-decoration:none;color:#fff;font-weight:bold;font-family:微软雅黑;font-size:20px;">Yime易米网</a>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        <tr>
        <td bgcolor="#ffffff" align="center" style="padding: 0 15px 0px 15px;">
        <table border="0" cellpadding="0" cellspacing="0" width="480" class="responsive-table">
        <tbody><tr>
        <td>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tbody>
        <tr>
        <td>
        <table cellpadding="0" cellspacing="0" border="0" align="left" class="responsive-table">
        <tbody><tr>
        <td width="550" align="left" valign="top">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tbody><tr>
        <td bgcolor="#ffffff" align="left" style="background-color:#ffffff; font-size: 17px; color:#7b7b7b; padding:28px 0 0 0;line-height:25px;"><b>##EMAIL##，你好！</b>
        </td>
        </tr>
        <tr>
        <td align="left" valign="top" style="font-size:15px; color:#7b7b7b; font-size:14px; line-height: 25px; font-family:Hiragino Sans GB; padding: 15px 0px 25px 0px">Yime易米网已经收到了您重置密码的请求，请 24 小时内点击下面的按钮重置用户密码。
        </td>
        </tr>
        <tr>
        <td style="border-top:1px #f1f4f6 solid; padding: 26px 0 32px 0;" align="center" class="padding">
        <table border="0" cellspacing="0" cellpadding="0" class="responsive-table">
        <tbody><tr>
        <td>
        <span style="font-family:Hiragino Sans GB;;font-size:17px;color:#0a82e4">
        <a style="text-decoration:none;color:#ffffff;" href="##LINK##" target="_blank">
        <div style="padding:10px 25px 10px 25px;border-radius:3px;text-align:center;text-decoration:none;background-color:#59bb75;color:#ffffff;font-size:17px;margin:0;white-space:nowrap">重置密码
        </div>
        </a>
        </span>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>

        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tbody><tr>
        <td bgcolor="#f7f9fa" align="center">
        <table width="552" border="0" cellpadding="0" cellspacing="0" align="center" class="responsive-table">
        <tbody><tr>
        <td align="center" valign="top" bgcolor="#f7f9fa" style="font-family:Hiragino Sans GB; font-size:12px; color:#b6c2cc; line-height:17px; padding:0 0 25px 0;">这封邮件的收件地址是 <a href="mailto:##EMAIL##">##EMAIL##</a> <br>
        你可以通过<a href="" style="border:none;color:#8a939b;text-decoration:none;">&nbsp;设置&nbsp;</a><span>管理其他来自Yime易米网的邮件</span> <br>
        © 2014 Yime易米网
        </td>
        </tr>
        </tbody></table>
        </td>
        </tr>
        </tbody></table>
        <style type="text/css">
        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}
        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word}
        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
        img{ border:0}
        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
        </style>

        <style id="ntes_link_color" type="text/css">a,td a{color:#064977}</style></body></html>
EOT;
        $content=str_replace('##LINK##',base_url('home/resetpassword/'.$resetPwd_Token),$content);
        $content=str_replace('##EMAIL##',$sendTo,$content);
        return self::sendMail($sendTo, $subject, $content);
    }

}